//
//  ByNewsDetailVC.m
//  caipiao_ios
//
//  Created by mc on 2018/8/13.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByNewsDetailVC.h"

@interface ByNewsDetailVC ()<UITableViewDelegate,UITableViewDataSource,UIWebViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIWebView  *webView;
@property (nonatomic, strong) ByNetModel *detailModel;


@end

@implementation ByNewsDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftBackBarButtonItem];
    self.title = @"资讯详情";
    [self initViews];
    [self loadData];
    
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    self.tableView.mj_header = header;
    // Do any additional setup after loading the view.
}


- (void)initWebView{
    UIWebView  *webView =  [[UIWebView alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH - 20, 0)];;
    webView.delegate = self;
    webView.backgroundColor = [UIColor whiteColor];
    webView.scrollView.showsHorizontalScrollIndicator = NO;
    webView.scrollView.showsVerticalScrollIndicator = NO;
    webView.scrollView.scrollEnabled = NO;
    [webView loadHTMLString:self.detailModel.content baseURL:nil];
    [ByCoreToolAlert showLoadind:@"网页加载中"];
    self.webView = webView;
}

- (void)loadData{
    [ByCoreToolAlert showLoadind:@"加载中"];
    
    ByNetModel *netModel = [[ByNetModel alloc] init];
    netModel.article_id = self.netModel.article_id;
    [ByFoundApi newsInfo:netModel success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert hideHud];
        [self.tableView.mj_header endRefreshing];
        self.detailModel = [[ByNetModel alloc] initContentWithDic:result.data[@"data"]];
        [self initWebView];
        [self.tableView reloadData];
        SLog(@"资讯详情 = %@", result.data);
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
        [self.tableView.mj_header endRefreshing];
    }];
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [ByCoreToolAlert hideHud];
    CGFloat height = [[webView stringByEvaluatingJavaScriptFromString: @"document.body.scrollHeight"] intValue];
    webView.height = height;
    [self.tableView reloadData];
}

- (void)initViews{
    
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        if (@available(ios 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(0);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
        } else {
            make.top.equalTo(self.view).offset(0);
            make.bottom.equalTo(self.view).offset(0);
        }
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.contentView.backgroundColor = BY_BACKGROUND_COLOR;
        
    }
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    if (indexPath.row == 0) {
        
    UIView *backgroundView = [[UIView alloc] init];
    [cell.contentView addSubview:backgroundView];
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.contentView).offset(10);
        make.right.equalTo(cell.contentView).offset(-10);
        make.bottom.equalTo(cell.contentView).offset(-10);
        make.top.equalTo(cell.contentView).offset(0);
    }];
    backgroundView.layer.cornerRadius = 10;
    backgroundView.backgroundColor = [UIColor whiteColor];
    
    
    UILabel *titleLabel = [[UILabel alloc] init];
    [backgroundView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(10);
        make.top.equalTo(backgroundView).offset(10);
        make.height.mas_equalTo(@16);
        make.right.equalTo(backgroundView).offset(-10);
    }];
    
    titleLabel.font = FONT(14);
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.text = @"竞彩足球玩法介绍";
    
    
    UILabel *notesLabel = [[UILabel alloc] init];
    [backgroundView addSubview:notesLabel];
    [notesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(backgroundView).offset(-10);
        make.top.equalTo(titleLabel.mas_bottom).offset(10);
        make.size.mas_offset(CGSizeMake(60, 20));
    }];
    notesLabel.font = FONT(12);
    notesLabel.textColor = [UIColor lightGrayColor];
    notesLabel.textAlignment = NSTextAlignmentRight;
    notesLabel.text = @"阅读50";
    
    
    UILabel *timeLabel = [[UILabel alloc] init];
    [backgroundView addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel.mas_left).offset(0);
        make.bottom.equalTo(notesLabel.mas_bottom).offset(0);
        make.right.equalTo(notesLabel.mas_left).offset(-10);
        make.height.mas_equalTo(@20);
    }];
    timeLabel.text = @"2018-07-09 11:30";
    timeLabel.font = FONT(12);
    timeLabel.textColor = [UIColor lightGrayColor];
    
    
    timeLabel.text = [NSDate getDateStringFromeNumber:[NSNumber numberWithInteger:self.netModel.add_time.integerValue]];
    titleLabel.text = self.netModel.title;
    notesLabel.text = [NSString stringWithFormat:@"阅读%@", self.netModel.click];
        
    }else{
        
         [cell.contentView addSubview:self.webView];
        
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        
        return 70;
    }else{
         return _webView.height;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
